In this section, we’ll cover some of the important pytest.ini settings that are commonly used in testing.

Logging

[pytest]
log_cli=true
log_level=DEBUG
log_format = %(asctime)s %(levelname)s %(message)s
log_date_format = %Y-%m-%d %H:%M:%S

Output Console Logs To Log File

Perhaps you want to keep a record of all unit tests that ran as part of a CI/CD pipeline?

[pytest]
log_file = logs/pytest-logs.txt

Timeout Slow Tests

[pytest]
timeout=5

Define Environment Variables

You can define runtime environment variables within pytest.ini and share them across tests.

[pytest]
env =
    ENVIRONMENT=dev
    ACCOUNT=12345

Minimum Version

For example, I’ve specified that we need pytest==7.2 and actually installed pytest==7.1.3 .

[pytest]
minversion = 7.2

Set Required Plugins

Similarly, pytest-asyncio allows you to test asynchronous code. You can set the standards within your pytest.ini file like this

[pytest]
required_plugins = pytest-xdist>=3.2.0 pytest-env<=0.8.0

Use Custom Markers

[pytest]
addopts = --strict-markers
markers =
    core: marks tests as core (deselect with '-m "not core"')
    serial